** This do-file extracts a list of presidential elections from V-Dem

use "$project_path/data/1_input/elections/V-Dem/vdem_cd_extract.dta", clear

keep country_name year historical_date v2eltype_6 v2eltype_7

gen round = 1 if v2eltype_6==1
replace round = 2 if v2eltype_7==1
drop if round==.
sort country_name historical_date
by country_name : gen nextround = round[_n+1]
drop if round==1 & nextround==2

gen flag_two_round = 1 if round ==2
drop v2eltype* round nextround

//Keeping only elections between 1946 and 2018
ren year Year
keep if Year>1945
keep if Year<=2018

// Adjusting country names
replace country_name="Occupied Palestinian Territories"  if country_name=="Palestine/West Bank"
replace country_name="Bosnia-Herz"  if country_name=="Bosnia and Herzegovina"
replace country_name="C. Verde Is."  if country_name=="Cape Verde"
replace country_name="Cent. Af. Rep."  if country_name=="Central African Republic"
replace country_name="Comoro Is."  if country_name=="Comoros"
replace country_name="Czech Rep."  if country_name=="Czech Republic"
replace country_name="Congo (DRC)"  if country_name=="Democratic Republic of the Congo"
replace country_name="Dom. Rep."  if country_name=="Dominican Republic"
replace country_name="Eq. Guinea"  if country_name=="Equatorial Guinea"
replace country_name="FRG/Germany"  if country_name=="Germany"
replace country_name="Macedonia"  if country_name=="North Macedonia"
replace country_name="Congo"  if country_name=="Republic of the Congo"
replace country_name="Korea, Republic of"  if country_name=="South Korea"
replace country_name="Gambia"  if country_name=="The Gambia"
replace country_name="USA"  if country_name=="United States of America"
drop if country_name=="Republic of Vietnam"
drop if country_name=="Zanzibar"

// Month variable
gen Month = month(historical_date)
drop historical_date
ren country_name Country
order Country Year Month 
bys Country Year : gen temp=_n
bys Country Year : egen temp2 = max(temp)
replace Month = . if temp2==1
drop temp*

// Manual adjustments
replace Month = . if Country=="Austria" & Year==2016
replace Month = . if Country=="Ukraine" & Year==2004
replace Month = 8 if Country=="Korea, Republic of" & Year==1960
replace Country = "Serbia and Montenegro" if Country=="Serbia" & Year==2000
replace Month = . if Country=="Chile" & Year==2013 

duplicates drop

gen Type_Election="Presidential"

sort Country Year Month
order Country Year Month Type_Election flag_two_round
format Country %20s
compress
label data ""

lab var Country  "Country in which election took place"
lab var Year     "Year of the election"
lab var Month    "Month of the election"
lab var Type_Election    "Election type"
lab var flag_two_round   "Flags elections with two rounds"

save "$project_path/data/2_intermediary/elections/Presidential elections/pres_elec_vdem", replace
